.global	_start
_start:

LDR R1, =arr @ load bse address of arr[0] to r1
MOV R2, #10 @ load number of array element
MOV R3, #0  @ i = 0


BUBBLE_SORT:
CMP R3,R2   @ compare r2 and r3, if i == n ?
BGE END_OF_PROGRAM

MOV R4, #0  @ j = 0
OUTTER_LOOP:
SUB R10, R2, R3 @ n - i
SUB R10, R10, #1 @ n -1
CMP R4, R10 @ j == (n-i-1)?
BGE END_OF_INNER_LOOP
    INNER_LOOP:
    @ fetch arr[j] from memory
    LSL R5, R4, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD R8, R1, R5  @ add j*4 to base address of arr
    LDR R5, [R8]    @ load arr[j] to r5
    @ fetch arr[j+1] from memory
    ADD R6, R4, #1 @ j + 1
    LSL R6, R6, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD R9, R1, R6  @ add j*4 to base address of arr
    LDR R6, [R9]    @ load arr[j+1] to r6

    @ compare arr[j] and arr[j+1], branch if arr[j] is greater
    CMP R5, R6
    BGT SWAP_ELEMENT
    B SKIP_SWAP

    SWAP_ELEMENT:
    STR R5, [R9]
    STR R6, [R8]

    SKIP_SWAP:
    
    ADD R4, R4, #1 @ j++
    B OUTTER_LOOP
    

END_OF_INNER_LOOP:
ADD R3, R3, #1 @ i++
B BUBBLE_SORT



@ end of program
END_OF_PROGRAM:
LDR R1, =arr
LDR R0, [R1]


ADD R1, R1, #16
LDR R1, [R1]

END:
B END


.data
@		set 4 byte alignment
.balign	4
@ initialize arrary
arr: .4byte 5, 2, 3, 8, 1, 2, 6, 9, 3, 7
